package com.teamwings.ctrls.formCtrls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.teamwings.common.aspect.annotation.FormControl;
import com.teamwings.common.constant.FormConstant;
import com.teamwings.common.vo.Result;
import com.teamwings.ctrls.FormComponent;
import com.teamwings.modules.design.vo.FormVo;
import com.teamwings.modules.page.service.IPageService;
import lombok.Data;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;

import java.util.List;
import java.util.Map;


@Data
@FormControl(type = "fn_user_info")
public class UserInfo extends FormComponent {

    public UserInfo(){
        this.type = "fn_user_info";
        this.fieldType = "VARCHAR";
        this.fieldDefaultLength = "50";
    }

    @Override
    public Result<?> trigger(FormConstant.TRIGGER_TYPE type, IPageService pageService,
                             FormVo formDesignCode, Object paramsData) {
        if (type == FormConstant.TRIGGER_TYPE.CTRL_HANDLER) {
            JSONObject jsonObject = JSON.parseObject(paramsData.toString());
            String field = jsonObject.getString("field");
            LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

            String ret = "";

            switch (field) {
                case "realname":
                    ret = loginUser.getRealname();
                    break;
                case "phone":
                    ret = loginUser.getPhone();
                    break;
                case "email":
                    ret = loginUser.getEmail();
                    break;
                case "post":
                    ret = loginUser.getPost();
                    break;
                case "workNo":
                    List<Map> maps = pageService.executeSql("select work_no from sys_user where id='" + loginUser.getId() + "'");
                    ret = maps.get(0).get("work_no").toString();
                    break;
                case "orgCodeTxt":
                    List<Map> map = pageService.executeSql("select depart_name from sys_depart where org_code='" + loginUser.getOrgCode() + "'");
                    ret = map.get(0).get("depart_name").toString();
                    break;
                case "orgCode":
                    ret = loginUser.getOrgCode();
                    break;
                case "orgId":
                    String sql = "select id from sys_depart where org_code='" + loginUser.getOrgCode() + "'";
                    List result = pageService.executeSql(sql);
                    if (result != null && result.size() > 0) {
                        Map orgMap = (Map)result.get(0);
                        ret = orgMap.get("id") + "";
                    } else {
                        ret = "";
                    }
                    break;
                default:
                    ret = loginUser.getId();
            }

            return Result.OK(ret);
        }
        return null;
    }
}
